package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.dto.BackstageDTO;
import com.ktgj.customer.domain.vo.MallProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.service.IMallProductService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/MallProduct")
public class MallProductController extends BaseController
{
    @Autowired
    private IMallProductService mallProductService;

    /**
     * 查询商品列表
     */
    @PreAuthorize(hasPermi = "customer:product:list")
    @GetMapping("/list")
    public TableDataInfo list(MallProduct mallProduct)
    {
        startPage();
        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize(hasPermi = "customer:product:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProduct mallProduct) throws IOException
    {
        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        ExcelUtil<MallProduct> util = new ExcelUtil<MallProduct>(MallProduct.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    //@PreAuthorize(hasPermi = "customer:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return AjaxResult.success(mallProductService.selectMallProductById(productId));
    }

    /**
     * 新增商品
     */
    @PreAuthorize(hasPermi = "customer:product:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallProduct mallProduct)
    {
        return toAjax(mallProductService.insertMallProduct(mallProduct));
    }

    /**
     * 修改商品
     */
//    @PreAuthorize(hasPermi = "customer:product:edit")
//    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/editMallProduct")
    public AjaxResult edit(@RequestBody MallProduct mallProduct)
    {
        return toAjax(mallProductService.updateMallProduct(mallProduct));
    }

    /**
     * 删除商品
     */
    @PreAuthorize(hasPermi = "customer:product:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(mallProductService.deleteMallProductByIds(productIds));
    }

    /**
     * 商城首页
     * @return
     */
    @PostMapping("/mallHomepage")
    public AjaxResult mallHomepage() {
        MallHomePage mallHomePage = mallProductService.mallHomepage();
        return AjaxResult.success(mallHomePage);
    }

    /**
     * 分类展示
     * @param map
     * @return
     */
    @PostMapping("/selectMallProduct")
    public AjaxResult selectMallProduct(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        Long cId = Long.valueOf(map.get("cId").toString());
        List<MallProductVO> productVOList = mallProductService.selectMallProduct(id, cId);
        return AjaxResult.success(productVOList);
    }

    /**
     * 商城搜索
     * @param map
     * @return
     */
    @PostMapping("/productSearch")
    public AjaxResult productSearch(@RequestBody Map<String, String> map) {
        String type = map.get("type");
        String productName = map.get("productName");
        List<MallProductVO> productVOS = mallProductService.productSearch(type, productName);
        return AjaxResult.success(productVOS);
    }

    /**
     * 商品详情
     * @param map
     * @return
     */
    @PostMapping("/mallProductDetails")
    public AjaxResult mallProductDetails(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        MallProductDetails details = mallProductService.mallProductDetails(id);
        return AjaxResult.success(details);
    }

    /**
     * 后台商品列表
     * @param backstageDTO
     * @return
     */
    @PostMapping("/backstage")
    public TableDataInfo backstage(@RequestBody BackstageDTO backstageDTO) {
        startPage();
        List<MallProductBackstage> backstage = mallProductService.backstage(backstageDTO);
        return getDataTable(backstage);
    }

    /**
     * 商品类型
     * @return
     */
    @PostMapping("/mallProductTypeList")
    public AjaxResult mallProductTypeList() {
        return AjaxResult.success(mallProductService.mallProductTypeList());
    }

    /**
     * 后台商品详情
     * @param map
     * @return
     */
    @PostMapping("/productBackstageDetails")
    public AjaxResult productBackstageDetails(@RequestBody Map<String, Long> map) {
        Long productId = map.get("productId");
        MallProductBackstageDetails details = mallProductService.productBackstageDetails(productId);
        return AjaxResult.success(details);
    }

    /**
     * 修改后台商品详情
     * @param backstageDetails
     * @return
     */
    @PostMapping("/updateMallProduct")
    public AjaxResult updateMallProduct(@RequestBody MallProductBackstageDetails backstageDetails) {
        return toAjax(mallProductService.updateMallProduct(backstageDetails));
    }

    /**
     * 后台添加商品
     * @param product
     * @return
     */
    @PostMapping("/addMallProduct")
    public AjaxResult addMallProduct(@RequestBody MallProduct product) {
        return toAjax(mallProductService.addMallProduct(product));
    }

    /**
     * 后台删除商品
     * @param map
     * @return
     */
    @PostMapping("/deleteMallProduct")
    public AjaxResult deleteMallProduct(@RequestBody Map<String, Long> map) {
        Long productId = map.get("productId");
        return toAjax(mallProductService.deleteMallProduct(productId));
    }

    /**
     * 后台添加活动商品列表
     * @param map
     * @return
     */
    @PostMapping("/activityProduct")
    public TableDataInfo activityProduct(@RequestBody Map<String, Object> map) {
        startPage();
        Long typeId = Long.valueOf(map.get("typeId").toString());
        Long propertyTypeId = Long.valueOf(map.get("propertyTypeId").toString());
        String productName = String.valueOf(map.get("productName"));
        List<MallActivityProductADD> productADDList = mallProductService.activityProduct(typeId, propertyTypeId, productName);
        return getDataTable(productADDList);
    }
}
